package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.mapper.SysMenuMapper;
import cn.tedu.oa_system.pojo.SysMenu;
import cn.tedu.oa_system.pojo.in.FindLikeKeyIn;
import cn.tedu.oa_system.pojo.in.SysMenuIn;
import cn.tedu.oa_system.pojo.vo.SysMenuVO;
import cn.tedu.oa_system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;

/**
 * @Author MW
 * @Description 系统菜单服务接口实现类
 * @CreateDate 2023/3/8
 */
@Slf4j
@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public ResultDto<Void> addNew(SysMenuIn sysMenuIn) {
        SysMenu sysMenu=new SysMenu();
        BeanUtils.copyProperties(sysMenuIn,sysMenu);
        int row = sysMenuMapper.insert(sysMenu);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"添加系统菜单失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> delete(SysMenu sysMenu) {
        int row = sysMenuMapper.deleteById(sysMenu.getMenuId());
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"删除系统菜单失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> update(SysMenuIn sysMenuIn) {
        SysMenu sysMenu=new SysMenu();
        BeanUtils.copyProperties(sysMenuIn,sysMenu);
        int row = sysMenuMapper.updateById(sysMenu);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"修改系统菜单失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDataDto<SysMenuVO> list(FindLikeKeyIn likeKeyIn) {
        //先判断是否有模糊查询关键字
        //若没有  正常返回列表
        if (likeKeyIn.getLikeKey()==null){
            LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
            wrapper.ne(SysMenu::getMenuId,-1)
                    .eq(SysMenu::getParentId,0);
            List<SysMenu> menuParentList = sysMenuMapper.selectList(wrapper);
            LambdaQueryWrapper<SysMenu> wrapper1=new LambdaQueryWrapper<>();
            wrapper.ne(SysMenu::getMenuId,-1)
                    .ne(SysMenu::getParentId,0)
                    .ne(SysMenu::getParentId,null);
            List<SysMenu> menuSonList = sysMenuMapper.selectList(wrapper1);
            List<SysMenuVO> list=new ArrayList<>();
            SysMenuVO sysMenuVO=new SysMenuVO();
            for (int i = 0; i < menuParentList.size() - 1; i++) {
                BeanUtils.copyProperties(menuParentList.get(i),sysMenuVO);
                sysMenuVO.setType("父级");
                list.add(sysMenuVO);
                for (int j = 0; j < menuSonList.size()-1; j++) {
                    if (menuSonList.get(j).getParentId().equals(menuParentList.get(i).getMenuId())) {
                        BeanUtils.copyProperties(menuSonList.get(j),sysMenuVO);
                        sysMenuVO.setType("子级菜单");
                        list.add(sysMenuVO);
                    }
                }
            }
            ResultDataDto<SysMenuVO> resultDataDto=new ResultDataDto<>();
            resultDataDto.setDataList(list);
            resultDataDto.setIsSuccess(true);
            return resultDataDto;
        }

        //若有模糊查询关键字  则按关键字查询
        LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(SysMenu::getMenuName,likeKeyIn.getLikeKey())
                .ne(SysMenu::getMenuId,-1)
                .eq(SysMenu::getParentId,0);
        List<SysMenu> menuParentList = sysMenuMapper.selectList(wrapper);
        LambdaQueryWrapper<SysMenu> wrapper1=new LambdaQueryWrapper<>();
        wrapper.like(SysMenu::getMenuName,likeKeyIn.getLikeKey())
                .ne(SysMenu::getMenuId,-1)
                .ne(SysMenu::getParentId,0)
                .ne(SysMenu::getParentId,null);
        List<SysMenu> menuSonList = sysMenuMapper.selectList(wrapper1);
        List<SysMenuVO> list=new ArrayList<>();
        SysMenuVO sysMenuVO=new SysMenuVO();
        for (int i = 0; i < menuParentList.size() - 1; i++) {
            BeanUtils.copyProperties(menuParentList.get(i),sysMenuVO);
            sysMenuVO.setType("父级");
            list.add(sysMenuVO);
            for (int j = 0; j < menuSonList.size()-1; j++) {
                if (menuSonList.get(j).getParentId().equals(menuParentList.get(i).getMenuId())) {
                    BeanUtils.copyProperties(menuSonList.get(j),sysMenuVO);
                    sysMenuVO.setType("子级菜单");
                    list.add(sysMenuVO);
                }
            }
        }
        ResultDataDto<SysMenuVO> resultDataDto=new ResultDataDto<>();
        resultDataDto.setDataList(list);
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDto<Void> upOrDown(SysMenuIn sysMenuIn) {
        //先判断是什么操作   handle值为0则是上移,若为1则是下移
        if (sysMenuIn.getHandle()==0){
            int row = sysMenuMapper.up(sysMenuIn.getMenuId());
            Assert.isTrue(row==DATABASE_AFFECTED_ROW,"菜单上移失败");
        }
        if (sysMenuIn.getHandle()==1){
            int row = sysMenuMapper.down(sysMenuIn.getMenuId());
            Assert.isTrue(row==DATABASE_AFFECTED_ROW,"菜单下移失败");
        }
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }
}
